package com.bjpowernode.crm.web.controller;

import com.bjpowernode.crm.constants.Constants;
import com.bjpowernode.crm.pojo.Value;
import com.bjpowernode.crm.service.ValueService;
import com.bjpowernode.crm.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("value")
public class ValueController {

    @Autowired
    private ValueService valueService;

    @GetMapping("index.html")
    public ModelAndView index() {
        List values = valueService.getAll();

        ModelAndView mv = new ModelAndView();
        mv.addObject("values", values);
        mv.setViewName("settings/dictionary/value/index");

        return mv;
    }

    @PostMapping("save.do")
    public String save(Value value) {
        value.setId(UUIDUtils.getUUID());
        valueService.save(value);
        return "redirect:index.html";
    }

    @GetMapping("edit.html")
    public ModelAndView edit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("value", valueService.get(id));
        mv.setViewName("settings/dictionary/value/edit");
        return mv;
    }

    @PostMapping("edit.do")
    public String edit(Value value) {
        valueService.edit(value);
        return "redirect:index.html";
    }

    @GetMapping("del.do")
    public String del(String[] id) {
        valueService.del(id);
        return "redirect:index.html";
    }

    @GetMapping("changeOrderNo.do")
    @ResponseBody
    public Map changeOrderNo(String id, String orderNo) {
        valueService.changeOrderNo(id, orderNo);
        return Constants.Result.SUC;
    }

    @GetMapping("changeOrderNos.do")
    @ResponseBody
    public Map changeOrderNos(String[] ids) {
        valueService.changeOrderNos(ids);
        return Constants.Result.SUC;
    }
}
